﻿myApp.controller("ProductCtrl", ['$scope', '$http', '$routeParams', '$restUrls', '$post', '$rootScope', function ($scope, $http, $routeParams, $restUrls, $post, $rootScope) {
    
    //f62a33b2b08b48c588590c1c73fe4f5f
    var product_id = $routeParams.id;
    $scope.Product = {};
    $scope.Product_Attribute = [];
    $scope.Images = [];
    $scope.Product_Reviews = [];
    $scope.Special = {};
    $scope.sale = 0;
    $scope.istimeshow = false;
    $scope.issoldout = false;
    $scope.GetProductDetail = function (product_id) {
        $http({ method: "GET", url: $restUrls["GetProductDetail"], params: { product_id: product_id } }).success(function (result) {
            if (result.success == 1) {
                $scope.Images = result.data.images;
                $scope.Product = result.data.product;

                //微信分享

                wx.ready(function () {
                    wx.onMenuShareTimeline({
                        title: $scope.Product.name, // 分享标题
                        link: window.location.href, // 分享链接
                        imgUrl: '', // 分享图标
                        success: function () {
                            // 用户确认分享后执行的回调函数
                            $.post("http://yxp.shicifang.cc/api/index.php?route=account/wxshare/addRecord", { link: link });
                            $.post("http://yxp.shicifang.cc/api/index.php?route=account/reward/updateReward", { points: 1,description:'积分商品分享赠送1积分' });
                        },
                        cancel: function () {
                            // 用户取消分享后执行的回调函数
                        }
                    });
                    wx.onMenuShareAppMessage({
                        title: $scope.Product.name, // 分享标题
                        desc: '', // 分享描述
                        link:  window.location.href, // 分享链接
                        imgUrl: '', // 分享图标
                        type: '', // 分享类型,music、video或link，不填默认为link
                        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                        success: function () {
                            // 用户确认分享后执行的回调函数
                            $.post("http://yxp.shicifang.cc/api/index.php?route=account/wxshare/addRecord", { link: link });
                            $.post("http://yxp.shicifang.cc/api/index.php?route=account/reward/updateReward", { points: 1,description:'积分商品分享赠送1积分' });
                        },
                        cancel: function () {
                            // 用户取消分享后执行的回调函数
                        }
                    });
                })

                $scope.sale = $scope.Product.current_price;
                $scope.Product_Reviews = result.data.reviews.metas;
                if (result.data.product_attribute.length > 0) {
                    $scope.Product_Attribute = result.data.product_attribute[0];
                }
                $scope.Special = result.data.sepcial;
                if ($scope.Special.product_special_id) {
                    if ($scope.Special.qunatity > 0) {
                        $scope.istimeshow = true;
                        $scope.sale = $scope.Special.price;
                    } else {
                        $scope.issoldout = true;
                    }
                    function GetRTime() {
                        if (!$("#time").length) {
                            window.clearInterval(timer);
                            return;
                        }
                        var EndTime = new Date($scope.Special.date_end.replace(/-/g, "/"));
                        var NowTime = new Date();
                        var t = EndTime.getTime() - NowTime.getTime();
                        var d = 0;
                        var h = 0;
                        var m = 0;
                        var s = 0;
                        if (t >= 0) {
                            d = Math.floor(t / 1000 / 60 / 60 / 24);
                            h = Math.floor(t / 1000 / 60 / 60 % 24);
                            m = Math.floor(t / 1000 / 60 % 60);
                            s = Math.floor(t / 1000 % 60);
                        } else {
                            $scope.$apply(function () {
                                $scope.istimeshow = false;
                                $scope.sale = $scope.Product.current_price;
                                window.clearInterval(timer);
                            })
                        }
                        $("#time").text(d + "天" + h + "时" + m + "分" + s + "秒");
                    }
                    timer = setInterval(GetRTime, 0);
                }
                if ($scope.Images.length > 0) {
                    setTimeout(function () {
                        var mySwiper = new Swiper('.swiper-container', {
                            pagination: '.swiper-pagination',
                            autoplay: 2000,
                            speed: 300
                        })
                    }, 100);
                }
            }
        }).error(function (data, status, headers, config) {
            if (result.success == 1) {
                $scope.Product = result.data.product;
                $scope.Images = result.data.images;
                setTimeout(function () {
                    var swiper1 = new Swiper('.swiper3', {
                        pagination: '.swiper-pagination',
                        slidesPerView: 'auto',
                        centeredSlides: true,
                        paginationClickable: true,
                        spaceBetween: 0
                    });
                }, 50);

            }
        });
    }
    $scope.GetProductDetail(product_id);
    $scope.GetUserDefine4= function () {
        $http({ method: "GET", url: $restUrls["getUserDefine"], params: {user_define_id:4} }).success(function (result) {
            if(result.success==1){
                $scope.UserDefine4 = result.data;

            }
        }).error(function (data, status, headers, config) {

        });
    }

    $scope.GetUserDefine4();
    $scope.Like = function () {
        $http({ method: "GET", url: $restUrls["LikeProduct"], params: { product_id: product_id } }).success(function (result) {
            if (result.success == 1) {
                $.scojs_message('点赞成功!', $.scojs_message.TYPE_OK);
                $scope.Product.islike = true;
            }
        })
    }

    //抽奖
    $scope.Lottery = function (product) {
        $http({ method: "GET", url: $restUrls["getCustomerInfo"], params: {} }).success(function (result) {
            if (result.success == 1) {
                if (parseInt(product.ean) > parseInt(result.data.customer_points)) {
                    $.scojs_message('积分余额不足！', $.scojs_message.TYPE_ERROR);
                } else {
                    $scope.tipresult = true;
                    $post($restUrls["updateCustomerPoints"], { description: '抽取商品消耗积分', points: '-' + parseInt(product.ean) }).success(function (result) {
                    });
                    var x = Math.floor(Math.random() * parseInt(product.jan) + 1);
                    setTimeout(function () {
                        $scope.$apply(function () {
                            $scope.tipresult = false;
                            if (x == 1) {
                                $scope.successshow = true;
                                window.localStorage.setItem('isgetproduct', product.product_id);
                            } else {
                                $scope.failshow = true;
                            }
                        });
                    }, 2000);
                }
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }

    $scope.Retry = function (product) {
        $scope.failshow = false;
        $scope.Lottery(product);
    }


   
}]).controller("ProductListCtrl", ['$scope', '$http', '$routeParams', '$restUrls', '$post', '$rootScope', function ($scope, $http, $routeParams, $restUrls, $post, $rootScope) {
    var page = 1;
    $scope.Products = [];
    $http({ method: "GET", url: $restUrls["loadProductListByCategory"], params: { page: 1, limit: 10 } }).success(function (result) {
        if (result.success == 1) {
            $scope.Products = result.data.products;
        }
    }).error(function (data, status, headers, config) {
        $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
    });

    $('#productlist').dropload({
        scrollArea: window,
        loadUpFn: function (me) {
            page = 1;
            $http({ method: "GET", url: $restUrls["loadProductListByCategory"], params: { page: page, limit: 10 } }).success(function (result) {
                if (result.success == 1) {
                    $scope.Products = result.data.products;
                    me.resetload();
                } else {
                    me.resetload();
                };
            });
        },
        loadDownFn: function (me) {
            page = page + 1;
            $http({ method: "GET", url: $restUrls["loadProductListByCategory"], params: { page: page, limit: 10 } }).success(function (result) {
                if (result.success == 1) {
                    for (var i = 0; i < result.data.products.length; i++) {
                        $scope.Products.push(result.data.products[i]);
                    }
                    me.resetload();
                } else {
                    page = page - 1;
                    me.resetload();
                };
            }).error(function (data, status, headers, config) {
                page = page - 1;
                me.resetload();
            });
        }
    });
    $scope.GoProduct = function (product) {
        window.location.href = "#/product/" + product.product_id;
    }
}]).controller("ShopCtrl", ['$scope', '$http', '$routeParams', '$restUrls', '$post', '$rootScope', function ($scope, $http, $routeParams, $restUrls, $post, $rootScope) {
    


    $http({ method: "GET", url: $restUrls["loadProductListByCategory"], params: { page: 1, limit: 6 } }).success(function (result) {
        if (result.success == 1) {
            $scope.newproducts = result.data.products;
        }
    }).error(function (data, status, headers, config) {
        $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
    });


    $http({ method: "GET", url: $restUrls["loadProductListByCategory"], params: { page: 1, limit: 6, filter_viewed: 1 } }).success(function (result) {
        if (result.success == 1) {
            $scope.hotproducts = result.data.products;
        }
    }).error(function (data, status, headers, config) {
        $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
    });


    $http({ method: "GET", url: $restUrls["loadOrdersProduct"], params: { page: 1, limit: 10} }).success(function (result) {
        if (result.success == 1) {
            $scope.saleproducts = result.data.products;
            setTimeout(function () {
                var swiper2 = new Swiper('.swiper2', {
                    slidesPerView: 1,
                    paginationClickable: true,
                    spaceBetween: 30,
                    slidesPerView: 4,
                    freeMode: true
                });
            },1000)
        }
    }).error(function (data, status, headers, config) {
        $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
    });



    $http({ method: "GET", url: $restUrls["getBannerImagesById"], params: { banner_id: 7 } }).success(function (result) {
        if (result.success == 1) {
            $scope.banners = result.data;
            setTimeout(function () {
                var swiper1 = new Swiper('.swiper1', {
                    pagination: '#pagination1',
                    slidesPerView: 'auto',
                    centeredSlides: true,
                    paginationClickable: true,
                    spaceBetween: 0
                });
            }, 1000);
        }
    });

    $scope.GoProduct = function (product) {
        window.location.href = "#/product/" + product.product_id;
    }

    $scope.GoBannerLink = function (banner) {
        window.location.href = banner.link;
    }
}]);